home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 106_01.zip / STRFUN.ASM < prev    next >
Assembly Source File  |  1993-06-26  |  3KB  |  139 lines

  1. ;                strfun.asm
  2. ;
  3. ;    Copyright (C) 1980, M J Maney
  4. ;
  5. ;    First created 8/25/80
  6. ;    Last revised 8/30/80 19:15
  7. ;
  8. ;    Herein are the basic string functions that C people like to use,
  9. ;    coded in assembler to make them as fast as can be.
  10. ;
  11. ;    NOTE that I have used .NE. in the comments in place of the usual C
  12. ;    symbol, because MAC sometimes objects to such usage.
  13. ;
  14.     maclib    crl
  15. ;
  16. ;
  17. ; * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  18. ;
  19. ;                string functions
  20. ;
  21. ;    strlen        strcmp        strcpy        strcat
  22. ;
  23. ; * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  24. ;
  25. ;
  26. ;    strlen(str)
  27. ;    char *str;
  28. ;
  29. ;    Returns the length of the string pointed to by str. It is assumed
  30. ;    that the string is terminated by '\0', and the terminator is NOT
  31. ;    counted as a character, so the storage length of a string s is
  32. ;    strlen(s) + 1.
  33. ;
  34.     PROC    STRLEN
  35.     lxi    d,0
  36.     lhld    ARG1        ;str to HL
  37.     mvi    a,0
  38. strlen1    cmp    m
  39.     BZ    strlen2        ;for (i=0; *str++ .NE. EOS; i++) ;
  40.     inx    h
  41.     inx    d
  42.     BRA    strlen1
  43. ;
  44. strlen2    xchg            ;return i;
  45.     ret
  46.     PEND    STRLEN
  47. ;
  48. ;
  49. ;    strcmp(sx,sy)
  50. ;    char *sx,*sy;
  51. ;
  52. ;    Returns 1, 0, or -1, depending upon the lexicographical relation
  53. ;    of the strings pointed to by sx and sy. The ordering is essentially
  54. ;    that which would be used if the strings were words being alphebetized
  55. ;    except that upper case and lower case are distinct, and digits and
  56. ;    punctuation are treated as significant characters. The relation
  57. ;    between any two characters is simply the relation between the bit
  58. ;    patterns that are used to represent them, treated as unsigned
  59. ;    integers. It is also assumed that the string terminator, '\0', has
  60. ;    a smaller value than any other character (in fact, its zero).
  61. ;
  62. ;    1 if sx > sy, 0 if sx == sy, or -1 if sx < sy
  63. ;
  64.     PROC    STRCMP
  65.     lhld    ARG1
  66.     xchg            ;sx to DE
  67.     lhld    ARG2        ;sy to HL
  68. ;
  69. strcmp1    ldax    d
  70.     cmp    m        ;while (*sy == (c = *sx)) {
  71.     BNZ    strcmp2
  72.     inx    h        ;    sy++;
  73.     inx    d        ;    sx++;
  74.     ora    a
  75.     BNZ    strcmp1        ;    if (c == EOS)
  76.     lxi    h,0        ;        return 0;
  77.     ret            ;    }
  78. strcmp2    lxi    h,1        ;if (*sx > *sy)
  79.     rnc            ;    return 1;
  80.     dcx    h        ;else
  81.     dcx    h
  82.     ret            ;    return -1;
  83.     PEND    STRCMP
  84. ;
  85. ;
  86. ;    strcpy(des,src)
  87. ;    char *des,*src;
  88. ;
  89. ;    Copies string pointed to by src into memory starting at des. No
  90. ;    provision is made for limiting the amount of stuff copied: it is
  91. ;    the user's responsibility to insure that the string being copied
  92. ;    will fit where it is being copied to.
  93. ;
  94.     PROC    STRCPY
  95.     lhld    ARG1
  96.     xchg            ;des to DE
  97.     lhld    ARG2        ;src to HL
  98. strcpy1    mov    a,m        ;do {
  99.     inx    h
  100.     stax    d
  101.     inx    d        ;    *des++ = c = *src++;
  102.     ora    a
  103.     BNZ    strcpy1        ;    }while (c .NE. EOS);
  104.     ret            ;return;
  105.     PEND    STRCPY
  106. ;
  107. ;
  108. ;    strcat(des,src)
  109. ;    char *des,*src;
  110. ;
  111. ;    Copies the string pointed to by src onto the tail end of the string
  112. ;    pointed to by des. This is equivalent to
  113. ;
  114. ;        strcpy(des+strlen(des),src)
  115. ;
  116. ;    and there is an identical lack of testing for overrun during the
  117. ;    copying process.
  118. ;
  119.     PROC    STRCAT
  120.     lhld    ARG2
  121.     xchg            ;src to DE
  122.     lhld    ARG1        ;des to HL
  123.     sub    a
  124. strcat1    cmp    m        ;while (*des++ .NE. EOS)
  125.     inx    h        ;    ;
  126.     BNZ    strcat1
  127.     dcx    h        ;des--;
  128. strcat2    ldax    d        ;do {
  129.     inx    d
  130.     mov    m,a
  131.     inx    h        ;    *des++ = c = *src++;
  132.     ora    a
  133.     BNZ    strcat2        ;    }while (c .NE. EOS);
  134.     ret            ;return;
  135.     PEND    STRCAT
  136. ;
  137. ;
  138.     end
  139.